package com.vilen.weixin.web.filter;

import com.vilen.basic.model.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Administrator on 2016/2/17.
 */
public class AuthFilter implements Filter{
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest)servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse)servletResponse;
        String url = httpServletRequest.getRequestURI();
        if(url.indexOf("resources")>0||url.indexOf("login")>0||url.indexOf("/wreceive")>0) {
            filterChain.doFilter(httpServletRequest,httpServletResponse);
            return;
        }else {
            User u = (User)httpServletRequest.getSession().getAttribute("user");
            if(u == null) {
                httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/user/login");
                return;
            }
            filterChain.doFilter(httpServletRequest,httpServletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
